import sys
import rclpy
from PyQt5 import QtWidgets
from .submodules.qt_plt import *
class MyWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.drag_x = 0
        self.old_x = 0
        # QtWidgets.QWidget.__init__(self, parent)
    # def mouseDoubleClickEvent(self, event):#鼠标点击
    #     self.showFullScreen()
    #     print('测试：double click')
    def keyPressEvent(self, event):
        print("按下：" + str(event.key()))
        # 举例
        if(event.key() == Qt.Key_Escape):
            if self.isFullScreen():
                self.showMaximized()
                print('测试：ESC')
            else:
                self.showFullScreen()

    def mouseMoveEvent(self, e):
        x = e.x()
        y = e.y()
        self.select_flag = False
        self.drag_x = x - self.old_x
        self.old_x = x
        text = "x: {0},  y: {1}".format(x, y)
        IN_AREA = x > 600 and x < 1400 and y > 134 and y < 400
        if IN_AREA:
            print("drag")
    def mouseReleaseEvent(self, a0: QtGui.QMouseEvent) -> None:

        self.drag_x=0
        self.old_x = a0.x()
        self.select_flag = True
        return super().mouseReleaseEvent(a0)
    def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None:
        self.drag_x=0
        self.old_x = a0.x()
        self.select_flag = True
        return super().mousePressEvent(a0)
  

class GUI():
    def __init__(self):
        try:
            self.app = QtWidgets.QApplication(sys.argv)
            # self.MainWindow = QtWidgets.QMainWindow()
            self.MainWindow = MyWindow()
            self.ui = Ui_MainWindow_add_logic(self.MainWindow)
            
            self.MainWindow.show()
            sys.exit(self.app.exec_())
        except RuntimeError as e:
            print(f"an error ocurred: {e}")
        finally:
            print("destroy nodes")
            # self.ui.destroy_nodes()
    def keyPressEvent(self, event): 
    #这里event.key（）显示的是按键的编码
        print("按下" + str(event.key()))
        # 举例，这里Qt.Key_A注意虽然字母大写，但按键事件对大小写不敏感
        if (event.key() == Qt.Key_Escape):
            self.MainWindow.showNormal()
            print('测试ESC')
        if (event.key() == Qt.Key_Enter):
            self.MainWindow.showFullScreen()
            print('测试Enter')  

def main(args=None):
    try:
        rclpy.init(args=args)
        global_gui = GUI()
        rclpy.shutdown()
    except KeyboardInterrupt:
        print("Keyboard interrupted")
    finally:
        print("main gui finished")


if __name__ == '__main__':
    main()
